import Tab from "vux/src/components/tab/tab.vue";
import TabItem from "vux/src/components/tab/tab-item.vue";
import dateFormat from "vux/src/tools/date/format.js";

import { addClass, removeClass } from "@/assets/js/dom.js";
import template from "./msgCenterTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    Tab,
    TabItem,
  },
  filters: {
    dateFormat,
  },
  data() {
    return {
      showList: [],
      type: 1,
      page: 1,
      pullUpLoad: true,
      msgDetailData: {},
    };
  },
  created() {
    this._getDataList();
  },
  methods: {
    activeCopy(e) {
      addClass(e.target, "activeScroll");
      document.addEventListener("touchmove", () => {
        removeClass(e.target, "activeScroll");
      });
      document.addEventListener("touchend", () => {
        removeClass(e.target, "activeScroll");
      });
    },
    pullingDown() {
      this.page = 1;
      this._getDataList();
    },
    pullingUp() {
      if (this.pullUpLoadFlag) {
        this.$refs.scroll.forceUpdate(false);
        return;
      }
      this.page += 1;
      this._getDataList();
    },
    _getDataList(index) {
      let vm = this;
      vm.pullUpLoadFlag = false;
      if (index) {
        if (vm.$refs.scroll) {
          vm.$refs.scroll.scrollTo(0, 0);
        }
        vm.type = index;
        vm.page = 1;
      }
      let params = {};
      params.token = localStorage.getItem("token");
      params.type = vm.type;
      params.page = vm.page;
      vm.$http
        .post(vm.urlRequest + "?m=api&c=message&a=dataList", params)
        .then((res) => {
          if (res.status == 0) {
            if (vm.page == 1) {
              vm.showList = res.list;
            } else {
              vm.showList = vm.showList.concat(res.list);
            }
            if (vm.page == Number(res.pageSum)) {
              vm.pullUpLoadFlag = true;
            }
          }
          vm.$vux.loading.hide();
        })
        .catch((err) => {
          vm.$vux.loading.hide();
          console.log(err);
        });
    },
    msgDetail(type, id, index) {
      let vm = this;
      let params = {};
      params.token = localStorage.getItem("token");
      params.type = type;
      params.id = id;
      vm.$vux.loading.show();
      vm.$http
        .post(vm.urlRequest + "?m=api&c=message&a=detail", params)
        .then((res) => {
          vm.$vux.loading.hide();
          if (res.status == 0) {
            vm.msgDetailData = res.list;
            if (vm.msgDetailData.title) {
              vm.$router.push(
                "/msgCenter/dataDetail?type=" + type + "&id=" + id
              );

              var obj = {};
              obj = vm.showList[index];
              obj.has_read = 1;
              vm.$set(vm.showList, index, obj);
            }
          }
        })
        .catch((err) => {
          vm.$vux.loading.hide();
          console.log(err);
        });
    },
  },
  watch: {},
  activated() {},
  deactivated() {},
};

// WEBPACK FOOTER //
// ./src/components/msgCenter/msgCenter.vue
